package com.project.service.base;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

/**
 * Created by TAO on 2016/3/25.
 */
@Service
public class CacheServiceImpl implements   CacheService {
    private Cache appTokenCache;

    @Autowired
    public CacheServiceImpl(CacheManager cacheManager) {
        this.appTokenCache = cacheManager.getCache("app-token-cache");
    }

    //添加 app token
    @Override
    public void addAppToken(String token, long  expirationTime){
        appTokenCache.put(token,expirationTime);
    }

    //删除 app token
    @Override
    public void removeAppToken(String token){
        appTokenCache.evict(token);
    }

    //获取app token
    @Override
    public Long getAppToken(String token){
        if(appTokenCache.get(token)!=null)
            return (Long)appTokenCache.get(token).get();
        else
            return null;
    }
}
